# coding:UTF-8
from django.db import models
from django.contrib import admin


class UserInfo(models.Model):
    status = models.CharField(max_length=10, default="True")
    # nickname=username
    username = models.CharField(max_length=30)
    real_name = models.CharField(max_length=30, default="朱锦辉", null=True, blank=True)
    user_id = models.IntegerField(default=0, null=True, blank=True)
    password = models.CharField(max_length=200, null=True, blank=True)
    login_token = models.CharField(max_length=200, null=True, blank=True)
    user_level = models.IntegerField(default=0)
    email = models.EmailField(null=True, blank=True, verbose_name="E-mail")
    tel_number = models.CharField(max_length=20, null=True, blank=True)
    qq_number = models.CharField(max_length=20, null=True, blank=True, verbose_name="QQ")
    weibo_number = models.CharField(max_length=30, null=True, blank=True)
    introduction = models.CharField(max_length=200, null=True, blank=True)
    head_img_url = models.CharField(max_length=150, null=True, blank=True)
    register_time = models.DateTimeField(auto_now_add=True)
    last_conn_time = models.DateTimeField(auto_now=True)
    createTime = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.username

    class Meta:
        db_table = "user_info"


class UserInfoAdmin(admin.ModelAdmin):
    list_display = ('real_name',
                    'pk',
                    'username',
                    'status',
                    'createTime')

    search_fields = ("username",
                     "real_name",
                     "pk")
    ordering = ('-createTime',)


class Message(models.Model):
    status = models.CharField(max_length=10, default="True")
    from_per = models.ForeignKey(UserInfo, related_name="msg_list", null=True, blank=True)
    ip_detail = models.CharField(max_length=300, null=True, blank=True)
    type = models.CharField(max_length=10)
    origin_content = models.CharField(max_length=100)  # 发起的部分内容
    origin_url = models.CharField(max_length=200)  # 发起的内容url
    content = models.CharField(max_length=200, default="", null=True, blank=True)  # 消息主体内容
    createTime = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return str(self.pk)

    class Meta:
        db_table = "message"


class MessageAdmin(admin.ModelAdmin):
    list_display = ("pk",
                    "status",
                    "from_per",
                    "type",
                    "createTime")
    ordering = ('-createTime',)

    def set_status_unuseful(modeladmin, request, queryset):
        for msg in queryset:
            msg.status = False
            msg.save()

    set_status_unuseful.short_description = "设置选中消息无效"

    actions = ["set_status_unuseful"]

admin.site.register(UserInfo, UserInfoAdmin)
admin.site.register(Message, MessageAdmin)
